package com.huahua.api.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huahua.api.entity.base.BaseWebsocketRequest;
import com.huahua.api.entity.base.SimpleWebsocketResponse;
import org.yeauty.pojo.Session;

/**
 * @author LoveHuahua
 * @date 2022年02月14日 23:35
 * @description believe in yourself
 */
public class WebSocketUtil {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 发送消息
     *
     * @param session              用户
     * @param baseWebsocketRequest 请求
     * @param str                  返回的param
     * @return
     */
    public static  boolean sendMessage(Session session, BaseWebsocketRequest baseWebsocketRequest, String str) {
        SimpleWebsocketResponse simpleWebsocketResponse = new SimpleWebsocketResponse();
        simpleWebsocketResponse.setUuid(baseWebsocketRequest.getUuid());
        simpleWebsocketResponse.setCommand(baseWebsocketRequest.getCommand());
        simpleWebsocketResponse.setSystem(baseWebsocketRequest.getSystem());
        simpleWebsocketResponse.setResult(str);
        try {
            session.sendText(objectMapper.writeValueAsString(simpleWebsocketResponse));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return true;
    }
}
